package tt.dz.biz.dao.oper;

import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.Authority;
import tt.dz.vo.OperListVO;
import tt.dz.vo.Operator;

@Repository("operDAO")
public class OperDAOImpl
  extends BaseDAO
{
  private static final String QUERY_OPER_BY_MOBILE = "select  manager_id as operId , mobile_no as mobileNo,password as passwordfrom pub_oper_list where is_valid = 1 and is_locked = 0 and mobile_no=:mobileNo";
  private static final String QUERY_AUTH_BY_MOBILE_SUBSYSID = "SELECT r.role_id as authorityId,r.role_name as authorityName from pub_oper_list oper, pub_oper_role sor ,pub_role rwhere oper.manager_id = sor.manager_id and sor.role_id = r.role_id and  sor.sys_code =:sysCode  and r.sys_code =:sysCodeand oper.is_valid = 1 and oper.is_locked = 0 and sor.is_valid = 1 and r.is_valid = 1and  oper.mobile =:mobileNo";
  public static final String QUERY_OPER_LIST_FORID = "select  manager_id as operId ,mobile_no  as mobileNo  from pub_oper_list where manager_id=:operId ";
  private static final String UPDATE_OPERLIST_NEED_CONDITION = "update  pub_oper_list set updated_by = :updatedBy , updated_time = :updatedTime ";
  
  public Operator queryOpterByID(String operId)
  {
    try
    {
      Operator oper = new Operator();
      oper.setOperId(operId);
      return (Operator)queryForObject("select  manager_id as operId ,mobile_no  as mobileNo, merchantCode from pub_oper_list where manager_id=:operId ", oper, Operator.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    catch (IncorrectResultSizeDataAccessException e)
    {
      return null;
    }
    catch (DataAccessException e)
    {
      LogUtil.error(e.getMessage(), e);
    }
    return null;
  }
  
  public Operator queryOpterByMobile(Operator oper)
  {
    return (Operator)queryForObject("select  manager_id as operId , mobile_no as mobileNo,password as password, merchantCode from pub_oper_list where is_valid = 1 and is_locked = 0 and mobile_no=:mobileNo", oper, Operator.class);
  }
  
  public List<Authority> queryAuthorityByMobileAndSubsysId(Operator oper)
  {
    return queryForList("SELECT r.role_id as authorityId,r.role_name as authorityName from pub_oper_list oper, pub_oper_role sor ,pub_role rwhere oper.manager_id = sor.manager_id and sor.role_id = r.role_id and  sor.sys_code =:sysCode  and r.sys_code =:sysCodeand oper.is_valid = 1 and oper.is_locked = 0 and sor.is_valid = 1 and r.is_valid = 1and  oper.mobile =:mobileNo", oper, Authority.class);
  }
  
  public int updateOperList(OperListVO oper)
  {
    StringBuilder sql = new StringBuilder("update  pub_oper_list set updated_by = :updatedBy , updated_time = :updatedTime ");
    if (null != oper.getEmail()) {
      sql.append(" ,email=:email");
    }
    if (null != oper.getName()) {
      sql.append(" ,name=:name");
    }
    if (null != oper.getGender()) {
      sql.append(" ,gender=:gender");
    }
    if (null != oper.getMobileNo()) {
      sql.append(" ,mobile_no=:mobileNo");
    }
    if (null != oper.getMerchantCode()) {
        sql.append(" ,merchantCode=:merchantCode");
      }
    sql.append("  where manager_id=:managerId");
    return save(sql.toString(), oper);
  }
}
